<?php

namespace App\Http\Controllers\Marketing;


use App\Model\Foundation\Teacher;
use App\Model\Marketing\Product;
use App\Model\Marketing\ProductLesson;
use App\Model\System\User;
use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Http\Controllers\MessagePacket;


class ProductController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取单一
     * @param $id
     * @return packetMessage
     */
    function get($id)
    {
        $id = intval($id);
        $obj = Product::find($id);
        $lessions = ProductLesson::where('productid',$id)->get();
        if($obj){
            $username = '';
            $user = User::find($obj->created_by);
            if($user){
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
            }
            $obj->created_by = $username;
            $obj->lessons = $lessions;
            $teacher = Teacher::find($obj->master);
            $obj->masterinfo  = $teacher;
            return $this->success($obj);

        }
        return $this->fail($obj, '数据不存在');
    }


    /**
     * 批量查询
     * @param Request $request
     * @return packetMessage
     */
    function gets(Request $request)
    {
        $status = Product::PUBLISHED;
        $query =  Product::whereNotNull('id');


        if($request->has('status')){
            $status = $request->get('status');
            $query->where('status', $status);
        }
        if($request->has('type')){
            $type = $request->get('type');
            $query->where('type', $type);
        }
        if($request->has('title')){
            $title = $request->get('title');
            $title = '%'.$title.'%';
            $query->where('title','like',$title);
        }
        if($request->has('coursetype')){
            $coursetype = $request->get('coursetype');
            $query->where('coursetype', $coursetype);
        }

        $query->where(function ($query) use ($request){
            if($request->has('name')){
                $name = $request->get('name');
                $master = Teacher::where('lastname','like','%'.$name.'%')->pluck('teacheruid')->first();
                $query->where('title', 'like' ,'%'.$name.'%')
                    ->orwhere('master', $master);
            }
        });

        if($request->has('isHot')){
            $ishot = $request->get('isHot');
            $query->where('isHot', $ishot);
        }
        if($request->has('master')){
            $master = $request->get('master');
            $query->where('master', $master);
        }

        # 需要／不需要 分页
        if($request->has('page')) {
            $limit = 30;
            if ($request->has('perPage')) {
                $limit = $request->get('perPage');
            }
            $products = $query->orderBy('updated_at','desc')->paginate($limit)->toArray();
            foreach ($products['data'] as $key => $product) {
                $teacher = Teacher::find($product['master']);
                $products['data'][$key]['masterinfo'] = $teacher;
                if($product['publishtime']){
                    $products['data'][$key]['publishtime'] = date('Y-m-d H:i:s',$product['publishtime']);
                }
                if($product['unpublishtime']){
                    $products['data'][$key]['unpublishtime'] = date('Y-m-d H:i:s',$product['unpublishtime']);
                }
                $user = User::find($products['data'][$key]['created_by']);
                if($user){
                    $username = $user->account;
                    if(empty($username)){
                        $username = $user->number;
                    }
                    if(empty($username)){
                        $username = $user->email;
                    }
                    $products['data'][$key]['created_by'] = $username;
                }
            }
            $data['list'] = $products['data'];
            unset($products['data']);
            $data['pager'] = $products;
        }else{
            $products = $query->get()->toarray();
            foreach ($products as $key => $product) {
                $teacher = Teacher::find($product['master']);
                if($teacher){
                    $products[$key]['masterinfo'] = $teacher;
                }
                $user = User::find($products[$key]['created_by']);
                $username = '';
                if($user){
                    $username = $user->account;
                    if(empty($username)){
                        $username = $user->number;
                    }
                    if(empty($username)){
                        $username = $user->email;
                    }
                }
                $products[$key]['created_by'] = $username;
            }
            $data = $products;
        }

        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }


    /**
     * 添加一个
     * @param Request $request
     * @return packetMessage
     */
    function  create(Request $request)
    {
        $obj = new Product($request->all());
        $obj->created_by = Token::user()->userid;
        $obj->updated_by = Token::user()->userid;
        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 更新某个
     * @param Request $request
     * @return packetMessage
     */
    function  update(Request $request, $id)
    {
        $obj = Product::find($id);
        $obj->update($request->all());
        $obj->updated_by = Token::user()->userid;
        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }

    /**
     * 取消发布
     * @param $id
     * @return packetMessage
     */
    function  unpublish($id)
    {
        $obj = Product::find($id);
        $obj->status = Product::UNPUBLISHED;
        $obj->unpublishtime = time();
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }
    }

    /**
     * 发布
     * @param $id
     * @return packetMessage
     */
    function  publish($id)
    {
        $obj = Product::find($id);
        $obj->status = Product::PUBLISHED;
        $obj->publishtime = time();
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }
    }

    /**
     * 禁用
     * @param $id
     * @return packetMessage
     */
    function  disable($id)
    {
        $obj = Product::find($id);
        $obj->status = Product::DISABLE;
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }
    }



    /**
     * 删除
     * @param $id
     * @return packetMessage
     */
    function  delete($id)
    {
        if(Product::destroy($id)){
            return $this->success(true);
        }else{
            return $this->fail(false,'删除失败');
        }

    }


    /**
     * 添加一个课节
     * @param Request $request
     * @param $id
     * @return packetMessage
     */
    function addLesson(Request $request, $id)
    {
        $fields = $request->all();
        $fields['productid'] = $id;
        $obj = new ProductLesson($fields);
        $obj->created_by = Token::user()->userid;
        $obj->updated_by = Token::user()->userid;
        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }

    /**
     * 更新一个课节
     * @param Request $request
     * @param $id
     * @return packetMessage
     */
    function updateLesson(Request $request, $pid, $id)
    {
        $fields = $request->all();
        $obj = ProductLesson::find($id);
        $obj->update($fields);
        $obj->created_by = Token::user()->userid;
        $obj->updated_by = Token::user()->userid;
        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }

    /**
     * 删除一个课节
     * @param Request $request
     * @param $id
     * @return packetMessage
     */
    function deleteLesson(Request $request, $pid, $id)
    {
        $obj = ProductLesson::find($id);
        if($obj->delete()){
            return $this->success(true);
        }else{
            return $this->fail(false,'保存失败');
        }
    }

    /**
     * 删除多个课节
     * @param Request $request
     * @param $id
     * @return packetMessage
     */
    function deleteLessons(Request $request, $pid)
    {
        try {
            if($request->has('lessonid')){
                ProductLesson::whereIn('id',$request->get('lessonid'))->delete();

                return $this->success(true,'删除成功');
            }else{
                return $this->fail(false,'参数不全');
            }
        }catch (\Exception $e) {
            return $this->fail(false,'删除失败'.$e->getMessage());
        }

    }


    /**
     * 获取所有课节
     * @param Request $request
     * @param $id
     * @return packetMessage
     */
    function getLessons(Request $request, $id)
    {
        $lessons = ProductLesson::where('productid', $id)->get();
        if($lessons){
            foreach ($lessons as &$lesson){
                $user = User::find($lesson->created_by);
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
                $lesson->created_by = $username;
                $user = User::find($lesson->updated_by);
                $username = $user->account;
                if(empty($username)){
                    $username = $user->number;
                }
                if(empty($username)){
                    $username = $user->email;
                }
                $lesson->updated_by = $username;
            }
            return $this->success($lessons);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


}
